layui.use(['element','table','form','layer'], function(){
    var table = layui.table,
        form = layui.form,
        $ = layui.jquery,
        layer = layui.layer;

    form.verify({
        readCount: function(value, item){
            var min = item.getAttribute('min-count');
            var max = item.getAttribute('max-count');
            if(value < Number(min) || value > Number(max) ){
                return '读取数据量为1-20';
            }
        },
        interval: function(value, item){
            var min = item.getAttribute('min-count');
            var max = item.getAttribute('max-count');
            if(value < Number(min) || value > Number(max) ){
                return '间隔时间为5-59秒';
            }
        }
    });

    function formatTime(data){
        return data ? data.replace("T", " "):"";
    }

    table.render({
        elem: '#deviceTable'
        ,url:'/cardpassmgrDevice/list'
        ,title: '设备管理'
        ,where: {}
        ,cols: [[
            {field: 'xh',type:'numbers', title: '序号',}
            //,{type:'checkbox'}
            ,{field:'nickName', title:'设备名称',align:'center'}
            ,{field:'deviceMac', title:'设备MAC',align:'center'}
            ,{field:'configStatus', title:'配置状态',align:'center',templet:function (data) {
                    return parseConfigStatus(data.configStatus);
                }}
            ,{field:'configUser', title:'配置人员',align:'center'}
            ,{field:'updateTime', title:'最后配置时间',  sort: true,templet:
                function(data){
                    return formatTime(data.updateTime);
                }}
            ,{field:'right',title:'操作',toolbar:'#barOperate',align:'center',width:220}
        ]]
        ,page: true
    });

    table.on('tool(deviceTable)',function (obj) {
        var data = obj.data;
        switch (obj.event) {
            case 'config':
                $("#config_form").attr("disabled", false);
                $("#config_form").attr("class", "layui-btn");
                $("#configId").val(data.id);
                form.val("paramconfig_form", {
                    "nickName": data.nickName
                    ,"location": data.location
                    ,"serialNum": data.serialNum
                    ,"password": data.password
                    ,"recordCode": data.recordCode
                    ,"recordIndex": data.recordIndex
                    ,"readCount": data.readCount
                    ,"intervalSecond": data.intervalSecond
                });
                form.render();
                layer.open({
                    type: 1
                    , title: '设备设置'
                    , area: ['60%', '85%']
                    , content: $('#configDiv')
                });
                break;
            case 'delete':
                layer.confirm('确定要删除吗?', {
                }, function(){
                    layer.load();
                    $.get('/cardpassmgrDevice/delete',{id:data.id},function (res) {
                        layer.msg(res.msg,{
                            time:500,
                            end:function () {
                                table.reload('deviceTable');
                                layer.closeAll();
                            }
                        });
                    });
                });
                break;

        }
    });

    form.on('submit(queryDevice)',function () {
        var nickName = $("#searcheNickName").val();
        var configStatus = $("#searchConfigStatus").val();
        table.reload('deviceTable',{
            where: {nickName:nickName,configStatus:configStatus}
            ,page: {curr:1}
        });
        return false;
    });

    form.on('submit(config_form)',function(data){
        $("#config_form").attr("disabled", true);
        $("#config_form").attr("class", "layui-btn layui-btn-disabled");
        layer.load();
        $.ajax({
            type: 'post',
            url: '/cardpassmgrDevice/saveOrUpdate',
            contentType: "application/json;charset=utf-8",
            dataType: 'json',
            data: JSON.stringify(data.field),
            success: function (res) {
                layer.msg(res.msg);
                setTimeout(function (){
                    layer.closeAll();
                },1000);
                $("#config_form").attr("disabled", false);
                $("#config_form").attr("class", "layui-btn");
                table.reload('deviceTable');
            },
        });
        return false;
    });

    $("#cancel_config").on('click',function (){
        layer.closeAll();
        return false;
    });

    $("#syncDevice").on('click', function (){
        layer.confirm('同步数据，是否继续？', {
            btn: ['确认', '取消'] //按钮
        }, function () {
            var index = layer.load(2, {
                shade: [0.4, '#000'],
                content: '同步中...',
                success: function (layero) {
                    layero.find('.layui-layer-content').css({
                        'padding-top': '40px',
                        'width': '200px'
                    });
                }
            });
            $.post('/cardpassmgrDevice/syncData',{},function (res) {
                if(res.status === 200){
                    layer.msg(res.msg,{
                        time:1000,
                        end:function () {
                            reload();
                        }
                    });
                }else{
                    layer.msg(res.msg);
                }
                layer.close(index);
            });
        });
    });

});

function parseConfigStatus(status) {
    if(typeof status == "undefined" || status == null){
        return "未知";
    }
    switch (Number(status)) {
        case 0: return '未配置';
        case 1: return '已配置';
        default: return "未知";
    }
}

function reload(){
    location.reload();
}
